import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:starfish_tenement_app/pages/mine/collect/my_collect_vm.dart';
import 'package:starfish_tenement_app/pages/news/news_type/news_type_page.dart';

import '../../../api/models/app_news_data.dart';
import '../../../component/styles/app_colors.dart';
import '../../../component/title/app_text.dart';
import '../../../utils/string_utils.dart';

///资讯收藏
class MyCollectNewsPage extends StatefulWidget {
  const MyCollectNewsPage({super.key});

  @override
  State createState() {
    return _MyCollectNewsPageState();
  }
}

class _MyCollectNewsPageState extends State<MyCollectNewsPage> {
  final MyCollectViewModel _viewModel = MyCollectViewModel();

  @override
  void initState() {
    super.initState();
    _viewModel.getCollectList(2);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MyCollectViewModel>(
        create: (context) {
          return _viewModel;
        },
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            title: const Text("资讯收藏"),
          ),
          body: SafeArea(
              child: Expanded(
            child: Padding(
                padding: EdgeInsets.only(left: 17.w, right: 14.w),
                child: Consumer<MyCollectViewModel>(
                  builder: (context, vm, child) {
                    return ListView.builder(
                        itemCount: vm.newsList?.length ?? 0,
                        itemBuilder: (context, index) {
                          var item = vm.newsList?[index];
                          return newsItemView(item: item, onCollectTap: () {});
                        });
                  },
                )),
          )),
        ));
  }
}

Widget newsItemView({AppNewsItemData? item, GestureTapCallback? onCollectTap}) {
  return Container(
      width: double.infinity,
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(color: AppColors.lineColorFA, width: 1.r))),
      padding: EdgeInsets.only(top: 15.h, bottom: 14.h),
      child: Row(children: [
        Expanded(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          AppText(
              text: item?.title ?? "",
              fontSize: 20.sp,
              textColor: AppColors.textColor20,
              maxLines: 2),
          //三张图
          if (item?.imageList?.length == 3)
            Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: 20.h),
                height: 90.h,
                child: Row(children: [
                  Expanded(
                      flex: 1,
                      child: Image.network(
                          StringUtils.takeStrForList(item?.imageList),
                          fit: BoxFit.fill)),
                  7.horizontalSpace,
                  Expanded(
                      flex: 1,
                      child: Image.network(
                          StringUtils.takeStrForList(item?.imageList, index: 1),
                          fit: BoxFit.fill)),
                  7.horizontalSpace,
                  Expanded(
                      flex: 1,
                      child: Image.network(
                          StringUtils.takeStrForList(item?.imageList, index: 2),
                          fit: BoxFit.fill)),
                ])),
          20.verticalSpace,
          Row(children: [
            AppText(
                text: "大谈房屋知识 2019.08.08",
                fontSize: 12.sp,
                textColor: AppColors.textColorB7),
            const Expanded(child: SizedBox()),
            Image.asset("assets/images/icon_news_type_zhuanfa.png",
                width: 13.r, height: 13.r),
            13.horizontalSpace,
            Image.asset("assets/images/icon_news_type_dianzan.png",
                width: 13.r, height: 13.r),
            13.horizontalSpace,
            GestureDetector(
                onTap: onCollectTap,
                child: Image.asset(
                  "assets/images/icon_news_type_collect.png",
                  width: 13.r,
                  height: 13.r,
                  color:
                      item?.collected == true ? AppColors.collectColor : null,
                ))
          ])
        ])),
        //单张图
        if (item?.imageList?.length == 1)
          Container(
              padding: EdgeInsets.only(left: 14.w, right: 3.w),
              child: Image.network(StringUtils.takeStrForList(item?.imageList),
                  width: 100.w, height: 86.h,
                  fit: BoxFit.fill))
      ]));
}
